/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package cn.hxcx.top.modules.dvc.web;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.hxcx.top.common.config.Global;
import cn.hxcx.top.common.persistence.Page;
import cn.hxcx.top.common.utils.StringUtils;
import cn.hxcx.top.common.web.BaseController;
import cn.hxcx.top.modules.dvc.entity.DeviceInfo;
import cn.hxcx.top.modules.dvc.entity.DeviceUse;
import cn.hxcx.top.modules.dvc.service.DeviceInfoService;
import cn.hxcx.top.modules.dvc.service.DeviceManaService;
import cn.hxcx.top.modules.dvc.service.DeviceUseService;
import cn.hxcx.top.modules.sys.utils.UserUtils;

/**
 * 设备借用Controller
 * @author DoubleX
 * @version 2019-07-13
 */
@Controller
@RequestMapping(value = "${adminPath}/dvc/deviceUse")
public class DeviceUseController extends BaseController {

	@Autowired
	private DeviceUseService deviceUseService;
	@Autowired
	private DeviceInfoService deviceInfoService;
	@Autowired
	private DeviceManaService deviceManaService;
	
	@ModelAttribute
	public DeviceUse get(@RequestParam(required=false) String id) {
		DeviceUse entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = deviceUseService.get(id);
		}
		if (entity == null){
			entity = new DeviceUse();
		}
		return entity;
	}
	
	@RequiresPermissions("dvc:deviceUse:view")
	@RequestMapping(value = {"list", ""})
	public String list(DeviceUse deviceUse, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<DeviceUse> page = deviceUseService.findPage(new Page<DeviceUse>(request, response), deviceUse); 
		model.addAttribute("page", page);
		return "modules/dvc/deviceUseList";
	}
	
	@RequiresPermissions("dvc:deviceUse:view")
	@RequestMapping(value = {"mylist"})
	public String mylist(DeviceUse deviceUse, HttpServletRequest request, HttpServletResponse response, Model model) {
		deviceUse.setUser(UserUtils.getUser());
		Page<DeviceUse> page = deviceUseService.findPage(new Page<DeviceUse>(request, response), deviceUse); 
		model.addAttribute("page", page);
		return "modules/dvc/deviceUseMyList";
	}

	@RequiresPermissions("dvc:deviceUse:view")
	@RequestMapping(value = "form")
	public String form(DeviceUse deviceUse, Model model) {
		model.addAttribute("deviceUse", deviceUse);
		return "modules/dvc/deviceUseForm";
	}
	
	@RequiresPermissions("dvc:deviceUse:view")
	@RequestMapping(value = "borrow")
	public String borrow(DeviceUse deviceUse, Model model) {
		deviceUse.setUser(UserUtils.getUser());
		model.addAttribute("deviceUse", deviceUse);
		return "modules/dvc/deviceUseForm";
	}

	@RequiresPermissions("dvc:deviceUse:edit")
	@RequestMapping(value = "save")
	public String save(DeviceUse deviceUse, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, deviceUse)){
			return form(deviceUse, model);
		}
		DeviceInfo device = deviceInfoService.get(deviceUse.getDeviceId());
		device.setDeviceStatus("0");
		deviceInfoService.save(device);
		deviceUse.setBorrowStatus("0");
		deviceUse.setBorrowDate(new Date());
		deviceUseService.save(deviceUse);
		addMessage(redirectAttributes, "设备领用成功");
		return "redirect:"+Global.getAdminPath()+"/dvc/deviceUse/mylist?repage";
	}
	
	@RequiresPermissions("dvc:deviceUse:edit")
	@RequestMapping(value = "delete")
	public String delete(DeviceUse deviceUse, RedirectAttributes redirectAttributes) {
		deviceUseService.delete(deviceUse);
		addMessage(redirectAttributes, "删除设备使用成功");
		return "redirect:"+Global.getAdminPath()+"/dvc/deviceUse/?repage";
	}
	
	@RequiresPermissions("dvc:deviceUse:edit")
	@RequestMapping(value = "userback")
	public String userback(DeviceUse deviceUse, RedirectAttributes redirectAttributes) {
		if(StringUtils.isNotBlank(deviceUse.getDeviceId()) && deviceManaService.hasSameDevice(deviceUse.getDeviceId())) {
			addMessage(redirectAttributes, "该设备正在维修中");
			return "redirect:"+Global.getAdminPath()+"/dvc/deviceUse/mylist?repage";
		}
		try {
			deviceUse.setBackDate(new Date());
			deviceUse.setBorrowStatus("1");
			deviceUseService.save(deviceUse);
			addMessage(redirectAttributes, "设备归还成功");
		}catch(Exception e) {
			addMessage(redirectAttributes, "设备归还失败");
		}
		return "redirect:"+Global.getAdminPath()+"/dvc/deviceUse/mylist?repage";
	}
	
	@RequiresPermissions("dvc:deviceUse:edit")
	@RequestMapping(value = "back")
	public String back(DeviceUse deviceUse, RedirectAttributes redirectAttributes) {
		try {
			DeviceInfo device = deviceInfoService.get(deviceUse.getDeviceId());
			device.setDeviceStatus("1");
			deviceInfoService.save(device);
			deviceUse.setBorrowStatus("2");
			deviceUseService.save(deviceUse);
			addMessage(redirectAttributes, "设备归还确认成功");
		}catch(Exception e) {
			addMessage(redirectAttributes, "设备归还确认失败");
		}
		return "redirect:"+Global.getAdminPath()+"/dvc/deviceUse/?repage";
	}

}